#include <rclcpp/rclcpp.hpp>
using namespace std::chrono_literals;
class RateCppNode: public rclcpp::Node
{
public:
  RateCppNode():Node("rate_cpp_node_cpp")
  {
    RCLCPP_INFO(this->get_logger(),"rate_cpp_node_cpp创建");
    //this->demo01_rate();
    //this->demo02_time();
    this->demo03_duration();
  }
private:
  void demo03_duration()
  {
    rclcpp::Duration d1(1s); // 5s
    rclcpp::Duration d2(2,500000000L);
    RCLCPP_INFO(this->get_logger(),"s=%.2f ns=%ld",d1.seconds(),d1.nanoseconds());
    RCLCPP_INFO(this->get_logger(),"s=%.2f ns=%ld",d2.seconds(),d2.nanoseconds());
  }
  void demo01_rate()
  {
    //rclcpp::Rate rate(500ms);
    rclcpp::Rate rate(2.0);//1s 2次 等价于500ms
    while(rclcpp::ok())
    {
      RCLCPP_INFO(this->get_logger(),"----------------");
      rate.sleep();
    }
  }
  void demo02_time()
  {
    rclcpp::Time time1(500000000L); // 5s
    rclcpp::Time time2(2,500000000L);
    rclcpp::Time time3=this->now();
    RCLCPP_INFO(this->get_logger(),"s=%.2f ns=%ld",time1.seconds(),time1.nanoseconds());
    RCLCPP_INFO(this->get_logger(),"s=%.2f ns=%ld",time2.seconds(),time2.nanoseconds());
    RCLCPP_INFO(this->get_logger(),"s=%.2f ns=%ld",time3.seconds(),time3.nanoseconds());

  }
};
int main(int argc, char ** argv)
{
    rclcpp::init(argc,argv);
    rclcpp::spin(std::make_shared<RateCppNode>());
    rclcpp::shutdown();
    return 0;
}